package com.atguigu.gulimall.ware.service.impl;

import com.atguigu.common.utils.MathUtils;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.ware.feign.ProductFeignService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.ware.dao.PurchaseDetailDao;
import com.atguigu.gulimall.ware.entity.PurchaseDetailEntity;
import com.atguigu.gulimall.ware.service.PurchaseDetailService;
import org.springframework.transaction.annotation.Transactional;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {

    @Autowired
    ProductFeignService productFeignService;

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        LambdaQueryWrapper<PurchaseDetailEntity> queryWrapper = new LambdaQueryWrapper<>();
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
            queryWrapper.and(wrapper -> {
                wrapper.eq(PurchaseDetailEntity::getId, key)
                        .or().eq(PurchaseDetailEntity::getPurchaseId, key)
                        .or().eq(PurchaseDetailEntity::getSkuId, key);
            });
        }
        String status = (String) params.get("status");
        if (!StringUtils.isEmpty(status)) {
            queryWrapper.eq(PurchaseDetailEntity::getStatus, status);
        }
        String wareId = (String) params.get("wareId");
        if (!StringUtils.isEmpty(wareId)) {
            queryWrapper.eq(PurchaseDetailEntity::getWareId, wareId);
        }

        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params), queryWrapper);

        return new PageUtils(page);
    }

    @Override
    public List<PurchaseDetailEntity> listDetailByPurchaseId(Long id) {
        LambdaQueryWrapper<PurchaseDetailEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PurchaseDetailEntity::getPurchaseId, id);

        List<PurchaseDetailEntity> purchaseDetailEntities = this.list(queryWrapper);
        return purchaseDetailEntities;
    }

    @Override
    @Transactional
    public boolean savePurchaseDetail(PurchaseDetailEntity purchaseDetail) {
        //远程调用获取sku商品信息
        R info = productFeignService.getById(purchaseDetail.getSkuId());
        Map<String, Object> map = (Map<String, Object>) info.get("skuInfo");
        //如果返回成功，则修改采购金额
        if(info.getCode() == 0 || info.getCode() == 200){
            BigDecimal price = MathUtils.getBigDecimal(map.get("price"));
            BigDecimal sum = new BigDecimal(purchaseDetail.getSkuNum());
            purchaseDetail.setSkuPrice(price.multiply(sum));
        } else {
            return false;
        }
        this.save(purchaseDetail);
        return true;
    }

}